--#include "data\config\equip\EquipEnchantConfig.lua" once

--[[装备附魔
]]
function EquipEnchant( sysarg, equipPtr, lock )
	if not equipPtr then
		return
	end

	if not lock then
		lock = 0			--属性类型不锁定
	end

	local itemid 		= Item.getItemProperty( sysarg, equipPtr, Item.ipItemID, 0)
	local itemType 		= Item.getItemProperty( sysarg, equipPtr, Item.ipItemType, 0)
	local equipCircle 	= Item.getItemProperty( sysarg, equipPtr, Item.ipItemNeedCircle, 0)
	local equipLevel  	= Item.getItemProperty( sysarg, equipPtr, Item.ipItemActorLevel) or 0

	local enchantCount 	= Item.getItemProperty( sysarg, equipPtr, Item.ipItemEnchantCount, 0)			--装备附魔次数
	local enchantAttr 	= Item.getItemProperty( sysarg, equipPtr, Item.ipItemEnchantAttr, 0)			--装备当前附魔属性
	--print("EquipEnchant, itemid="..itemid..", equipCircle="..equipCircle..", enchantCount="..enchantCount..", enchantAttr="..enchantAttr)

	local enchantCfg = nil
	if equipCircle > 0 then
		enchantCfg = GetEquipEnchantByEquipCircle( equipCircle )	--根据转数
	else
		enchantCfg = GetEquipEnchantByEquipLevel( equipLevel )		--根据等级
	end
	if not enchantCfg then
		return
	end

	--print( "EquipEnchant, attrNumMax="..smithAttrNumMax..", attrCount="..attrCount..", lockNum="..lockNum )
	local enchantConsumes = enchantCfg.enchantConsumes
	if not enchantConsumes then
		return 
	end

	local lockConsumes = nil
	if lock > 0 then
		lockConsumes = enchantCfg.lockConsumes
		if not lockConsumes then
			return
		end
	end

	if not CheckConsumeCond( sysarg, enchantConsumes ) then
		return 
	end

	if lockConsumes and not CheckConsumeCond( sysarg, lockConsumes ) then
		return 
	end

	local smithId 	= GetExquipSmithIdByEnchantCount( enchantCfg, enchantCount )
	if not smithId then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipEnchant002, ttFlyTip)
		return
	end

	if not DoConsumeCond( sysarg, enchantConsumes, 1, GameLog.clEquipEnchantFee, Lang.ScriptTips.EquipLog021  ) then
		return 
	end

	if lockConsumes and not DoConsumeCond( sysarg, lockConsumes, 1, GameLog.clEquipEnchantFee, Lang.ScriptTips.EquipLog021  ) then
		return 
	end

	local propIdx 		= 1
	local setEquip 		= false				--返回抽取的属性
	local oldAttrValue 	= enchantAttr
	local newEnchantAttr = Item.generateItemOneSmithAttr( sysarg, equipPtr, smithId, propIdx, lock, false, setEquip, oldAttrValue )

	local attrType, attrSign, attrValue = Item.parseAttrFromValue( newEnchantAttr, 0, 0, 0 )
	--print( "EquipEnchant, smithId="..smithId..", lock="..lock..", type="..attrType..", sign="..attrSign..", value="..attrValue )


	Item.setItemProperty( sysarg, equipPtr, Item.ipItemEnchantCount, enchantCount+1)			--附魔次数+1
	Item.setItemProperty( sysarg, equipPtr, Item.ipItemEnchantAttr, newEnchantAttr)				--新附魔属性

	local myEnchantLevel, myEnchantExp = Actor.getEnchantData( sysarg, 0, 0 )
	--print( "EquipEnchant, myEnchantLevel="..myEnchantLevel..", myEnchantExp="..myEnchantExp )
	local actorEnchantCfg = ActorEnchantCfg[myEnchantLevel+1]
	if not actorEnchantCfg then
		return
	end

	if actorEnchantCfg.addExp <= 0 or actorEnchantCfg.maxExp <= 0 then
		return
	end

	local newEnchangLevel 	= myEnchantLevel
	local newEnchangExp		= myEnchantExp + actorEnchantCfg.addExp
	if newEnchangExp >= actorEnchantCfg.maxExp then
		newEnchangLevel = myEnchantLevel + 1
		newEnchangExp	= 0
	end
	Actor.setEnchantData( sysarg, newEnchangLevel, newEnchangExp )
	Actor.sendEnchantData( sysarg )
	--Actor.collectOperate( sysarg, 0 ) -- 重新计算角色属性（不需要）
end

function GetEquipEnchantByEquipCircle( equipCircle )
	for i, enchantCfg in ipairs( EquipEnchantCfg ) do
		if enchantCfg.equipCircle[1] <= equipCircle and equipCircle <= enchantCfg.equipCircle[2] then
			return enchantCfg
		end
	end
	return nil
end

function GetEquipEnchantByEquipLevel( equipLevel )
	for i, enchantCfg in ipairs( EquipEnchantCfg ) do
		if enchantCfg.equipLevel[1] <= equipLevel and equipLevel <= enchantCfg.equipLevel[2] then
			return enchantCfg
		end
	end
	return nil
end

--[[
根据属性附魔次数，确定属性库
]]
function GetExquipSmithIdByEnchantCount( enchantCfg, enchantCount )
	for i, smithLibCfg in ipairs( enchantCfg.smithLib ) do
		if smithLibCfg.enchantCount[1] <= enchantCount and enchantCount <= smithLibCfg.enchantCount[2] then
			return smithLibCfg.smithId
		end
	end
	return 0
end

